<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="乘客姓名" prop="pname" v-show="$store.state.user.name == 'admin'">
        <el-input
          v-model="queryParams.pname"
          placeholder="请输入乘客姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="航班编号" prop="fno">
        <el-input
          v-model="queryParams.fno"
          placeholder="请输入航班编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出发日期" prop="ddate">
        <el-date-picker clearable
                        v-model="queryParams.ddate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择出发日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="出发城市" prop="dcity">
        <el-input
          v-model="queryParams.dcity"
          placeholder="请输入出发城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到达城市" prop="acity">
        <el-input
          v-model="queryParams.acity"
          placeholder="请输入到达城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-show="$store.state.user.name == 'admin'">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:tickets:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <!--        <el-button
                  type="success"
                  plain
                  icon="el-icon-edit"
                  size="mini"
                  :disabled="single"
                  @click="handleUpdate"
                  v-hasPermi="['system:tickets:edit']"
                >修改
                </el-button>-->
      </el-col>
      <!--      <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['system:tickets:remove']"
              >退票
              </el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:tickets:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ticketsList" @selection-change="handleSelectionChange">
      <el-table-column label="订单编号" align="center" prop="ticketId"/>
      <el-table-column label="乘客姓名" align="center" prop="pname"/>
      <el-table-column label="航班编号" align="center" prop="fno"/>
      <el-table-column label="座舱等级" align="center" prop="level"/>
      <el-table-column label="出发日期" align="center" prop="ddate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ddate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出发时间" align="center" prop="dtime"/>
      <el-table-column label="出发城市" align="center" prop="dcity"/>
      <el-table-column label="到达时间" align="center" prop="atime"/>
      <el-table-column label="到达城市" align="center" prop="acity"/>
      <el-table-column label="价格" align="center" prop="price"/>
      <el-table-column label="座位号" align="center" prop="seat"/>
      <el-table-column label="订单状态" align="center" prop="state"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="toSub(scope.row)"
            v-hasPermi="['system:tickets:remove']"
          >改签
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:tickets:remove']"
          >退票
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleCheck(scope.row)"
            v-hasPermi="['system:tickets:check']"
          >审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="乘客姓名" prop="pname">
          <el-input v-model="form.pname" placeholder="请输入乘客姓名"/>
        </el-form-item>
        <el-form-item label="航班编号" prop="fno">
          <el-input v-model="form.fno" placeholder="请输入航班编号"/>
        </el-form-item>
        <el-form-item label="座舱等级" prop="level">
          <el-input v-model="form.level" placeholder="请输入座舱等级"/>
        </el-form-item>
        <el-form-item label="出发日期" prop="ddate">
          <el-date-picker clearable
                          v-model="form.ddate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择出发日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出发时间" prop="dtime">
          <el-input v-model="form.dtime" placeholder="请输入出发时间"/>
        </el-form-item>
        <el-form-item label="出发城市" prop="dcity">
          <el-input v-model="form.dcity" placeholder="请输入出发城市"/>
        </el-form-item>
        <el-form-item label="到达时间" prop="atime">
          <el-input v-model="form.atime" placeholder="请输入到达时间"/>
        </el-form-item>
        <el-form-item label="到达城市" prop="acity">
          <el-input v-model="form.acity" placeholder="请输入到达城市"/>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格"/>
        </el-form-item>
        <el-form-item label="座位号" prop="seat">
          <el-input v-model="form.seat" placeholder="请输入座位号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listTickets, getTickets, delTickets, addTickets, updateTickets, checkTickets} from "@/api/system/tickets";
import Cookies from "js-cookie";

export default {
  name: "tickets",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      ticketsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pname: null,
        fno: null,
        level: null,
        dtime: null,
        ddate: null,
        dcity: null,
        atime: null,
        acity: null,
        price: null,
        seat: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      if (Cookies.get("username") !== "admin") {
        this.queryParams.pname = Cookies.get("username")
      }
      listTickets(this.queryParams).then(response => {
        this.ticketsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ticketId: null,
        pname: null,
        fno: null,
        level: null,
        ddate: null,
        dtime: null,
        dcity: null,
        atime: null,
        acity: null,
        price: null,
        seat: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ticketId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },


    toSub(row) {
      if (row.state !== "已预订") {
        this.$modal.msgError("改签失败，请核实订单状态！");
        return
      }
      const ticketIds = row.ticketId || this.ids;
      this.$modal.confirm('是否改签订单编号为"' + ticketIds + '"的订单？').then(function () {
      }).then(() => {
        this.$router.push({path: '/amend', query: {id: row.ticketId}});
      }).catch(() => {
      });
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ticketId != null) {
            updateTickets(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.state = "已预订"
            addTickets(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 退票按钮操作 */
    handleDelete(row) {
      if (row.state!=="已预订" &&row.state!=="已改签"){
        this.$modal.msgError("退票失败，请核实订单状态！");
        return
      }
      const ticketIds = row.ticketId || this.ids;
      this.$modal.confirm('是否退订订单编号为"' + ticketIds + '"的订单？').then(function () {
        return updateTickets({
          ticketId:row.ticketId,
          state:"退票待审核"
        })
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("退票申请成功，请等待管理员进行审核！");
      }).catch(() => {
      });
      /*const ticketIds = row.ticketId || this.ids;
      if (row.state!=="已预订" &&row.state!=="已改签"){
        this.$modal.msgError("退票失败，请核实订单状态！");
        return
      }
      this.$modal.confirm('是否确认退订订单管理编号为"' + ticketIds + '"的数据项？').then(function () {
        return delTickets(ticketIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("退票成功");
      }).catch(() => {
      });*/
    },
    /** 审核按钮操作 */
    handleCheck(row) {
      const ticketIds = row.ticketId || this.ids;
      let state
      if (row.state == "改签待审核") {
        state = "已改签"
      } else if (row.state == "退票待审核") {
        state = "已退票"
      } else {
        this.$modal.msgError("审核失败，请确认订单状态！");
        return
      }
      this.$modal.confirm('是否确认审核订单管理编号为"' + ticketIds + '"的数据项？').then(function () {
        return checkTickets(ticketIds, state);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("审核成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/tickets/export', {
        ...this.queryParams
      }, `tickets_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
